// initial state

const user = {
  namespaced: true,
  state: {
    // TODO 存邮箱有啥用吗 token没必要存lastTime吧 userinfo有啥用吗
    email: '',
    lastLogin: 0,
    token: {
      token: '',
      lastTime: 0
    },
    username: '',
    uid: 0,
    userprefix: '',
    userInfo: {}
  },
  getters: { // TODO 这些getters有必要吗 直接获得state
    getUsername: (state) => {
      return state.username;
    },
    getUid: (state) => {
      return state.uid;
    },
    getUserprefix: (state) => {
      return state.userprefix;
    }
  },
  actions: {
    // TODO 下面有个清除方法欸 不打算调用一下吗
    removeLogin(state) {
      state.token = {};
      state.username = '';
      state.email = '';
      state.uid = 0;
      state.userprefix = '';
    }
  },

  mutations: {
    // TODO 命名咋还一会儿大写字母 一会儿驼峰
    SET_DATA: (state, payload) => { // 从localStorage同步
      state.email = payload.email;
      state.username = payload.username;
      state.token = payload.token;
      state.uid = payload.uid;
      state.userprefix = payload.userprefix;
      state.lastLogin = payload.lastLogin;
    },
    clearData: (state) => {
      state.email = '';
      state.username = '';
      state.token = {};
      state.uid = 0;
      state.userprefix = '';
      state.userInfo = {};
      localStorage.removeItem('uid');
    }
  }
};

export default user;
